const { ErrorModule } = require('../classes/result')
const controller = require('../controller/user')

module.exports = async (ctx, next) => {
  const username = ctx.request.body.username
  if (!username) {
    ctx.body = new ErrorModule('未键入用户名')
    return
  }
  
  try {
    await controller.userCheck(username)
    await next()
  } catch(data) {
    ctx.body = new ErrorModule({
      state: data.state
    }, '用户名已存在')
  }
}